package com.smartfast4j.manager.modules.version.service.impl;

import com.smartfast4j.common.Constants;
import com.smartfast4j.manager.modules.version.service.VersionManagerService;
import com.smartfast4j.mapper.version.VersionManagerMapper;
import com.smartfast4j.pojo.version.VersionManager;
import com.smartfast4j.pojo.version.VersionManagerExample;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VersionManagerServiceImpl implements VersionManagerService {
    @Autowired
    private VersionManagerMapper versionManagerMapper;

    private static final Logger logger = LoggerFactory.getLogger(VersionManagerServiceImpl.class);

    public int countByExample(VersionManagerExample example) {
        int count = this.versionManagerMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    public VersionManager selectByPrimaryKey(String id) {
        return this.versionManagerMapper.selectByPrimaryKey(id);
    }

    public List<VersionManager> selectByExample(VersionManagerExample example) {
        return this.versionManagerMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String id) {
        return this.versionManagerMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(VersionManager record) {
        return this.versionManagerMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(VersionManager record) {
        return this.versionManagerMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(VersionManagerExample example) {
        return this.versionManagerMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(VersionManager record, VersionManagerExample example) {
        return this.versionManagerMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(VersionManager record, VersionManagerExample example) {
        return this.versionManagerMapper.updateByExample(record, example);
    }

    public int insert(VersionManager record) {
        return this.versionManagerMapper.insert(record);
    }

    public int insertSelective(VersionManager record) {
        return this.versionManagerMapper.insertSelective(record);
    }

    public int insertBatchSelective(List<VersionManager> records) {
        return this.versionManagerMapper.insertBatchSelective(records);
    }

    public int updateBatchByPrimaryKeySelective(List<VersionManager> records) {
        return this.versionManagerMapper.updateBatchByPrimaryKeySelective(records);
    }

    /**
     * 校验版本号
     * @param versionManager
     * @return
     */
    @Override
    public VersionManager checkVersionCode(VersionManager versionManager) {
        VersionManagerExample example = new VersionManagerExample();
        VersionManagerExample.Criteria criteria = example.createCriteria();
        criteria
                .andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO)
                .andVersionCodeEqualTo(versionManager.getVersionCode())
                .andTypeEqualTo(versionManager.getType())
                .andUpdateModeEqualTo(versionManager.getUpdateMode());
        if(StringUtils.isNotBlank(versionManager.getId())){
            criteria.andIdNotEqualTo(versionManager.getId());
        }
        List<VersionManager> list = this.selectByExample(example);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void delete(Long loginUserId, List<String> idList) {
        //删除
        VersionManager versionManager = new VersionManager();
        versionManager.setUpdateTime(new Date());
        versionManager.setUpdateUser(loginUserId);
        versionManager.setDeleteFlag(Constants.DELETE_FLAG.YES);
        VersionManagerExample example = new VersionManagerExample();
        example.createCriteria().andIdIn(idList);//要删除的ID
        updateByExampleSelective(versionManager,example);//逻辑删除
    }
}